package net.gyapp.markim.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import net.gyapp.markim.core.entity.MemberEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "MEMBER_ENTITY".
*/
public class MemberEntityDao extends AbstractDao<MemberEntity, Long> {

    public static final String TABLENAME = "MEMBER_ENTITY";

    /**
     * Properties of entity MemberEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property GroupId = new Property(1, Long.class, "groupId", false, "GROUP_ID");
        public final static Property FriendsId = new Property(2, Long.class, "friendsId", false, "FRIENDS_ID");
        public final static Property UserId = new Property(3, Long.class, "userId", false, "USER_ID");
        public final static Property MemberId = new Property(4, String.class, "memberId", false, "MEMBER_ID");
        public final static Property Avatar = new Property(5, String.class, "avatar", false, "AVATAR");
        public final static Property Name = new Property(6, String.class, "name", false, "NAME");
        public final static Property Nick = new Property(7, String.class, "nick", false, "NICK");
        public final static Property Remark = new Property(8, String.class, "remark", false, "REMARK");
        public final static Property Extra1 = new Property(9, String.class, "extra1", false, "EXTRA1");
        public final static Property Extra2 = new Property(10, String.class, "extra2", false, "EXTRA2");
        public final static Property Extra3 = new Property(11, String.class, "extra3", false, "EXTRA3");
    }

    private Query<MemberEntity> friendsEntity_ListQuery;
    private Query<MemberEntity> groupEntity_ListQuery;

    public MemberEntityDao(DaoConfig config) {
        super(config);
    }
    
    public MemberEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"MEMBER_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"GROUP_ID\" INTEGER," + // 1: groupId
                "\"FRIENDS_ID\" INTEGER," + // 2: friendsId
                "\"USER_ID\" INTEGER," + // 3: userId
                "\"MEMBER_ID\" TEXT," + // 4: memberId
                "\"AVATAR\" TEXT," + // 5: avatar
                "\"NAME\" TEXT," + // 6: name
                "\"NICK\" TEXT," + // 7: nick
                "\"REMARK\" TEXT," + // 8: remark
                "\"EXTRA1\" TEXT," + // 9: extra1
                "\"EXTRA2\" TEXT," + // 10: extra2
                "\"EXTRA3\" TEXT);"); // 11: extra3
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"MEMBER_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, MemberEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long groupId = entity.getGroupId();
        if (groupId != null) {
            stmt.bindLong(2, groupId);
        }
 
        Long friendsId = entity.getFriendsId();
        if (friendsId != null) {
            stmt.bindLong(3, friendsId);
        }
 
        Long userId = entity.getUserId();
        if (userId != null) {
            stmt.bindLong(4, userId);
        }
 
        String memberId = entity.getMemberId();
        if (memberId != null) {
            stmt.bindString(5, memberId);
        }
 
        String avatar = entity.getAvatar();
        if (avatar != null) {
            stmt.bindString(6, avatar);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(7, name);
        }
 
        String nick = entity.getNick();
        if (nick != null) {
            stmt.bindString(8, nick);
        }
 
        String remark = entity.getRemark();
        if (remark != null) {
            stmt.bindString(9, remark);
        }
 
        String extra1 = entity.getExtra1();
        if (extra1 != null) {
            stmt.bindString(10, extra1);
        }
 
        String extra2 = entity.getExtra2();
        if (extra2 != null) {
            stmt.bindString(11, extra2);
        }
 
        String extra3 = entity.getExtra3();
        if (extra3 != null) {
            stmt.bindString(12, extra3);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, MemberEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long groupId = entity.getGroupId();
        if (groupId != null) {
            stmt.bindLong(2, groupId);
        }
 
        Long friendsId = entity.getFriendsId();
        if (friendsId != null) {
            stmt.bindLong(3, friendsId);
        }
 
        Long userId = entity.getUserId();
        if (userId != null) {
            stmt.bindLong(4, userId);
        }
 
        String memberId = entity.getMemberId();
        if (memberId != null) {
            stmt.bindString(5, memberId);
        }
 
        String avatar = entity.getAvatar();
        if (avatar != null) {
            stmt.bindString(6, avatar);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(7, name);
        }
 
        String nick = entity.getNick();
        if (nick != null) {
            stmt.bindString(8, nick);
        }
 
        String remark = entity.getRemark();
        if (remark != null) {
            stmt.bindString(9, remark);
        }
 
        String extra1 = entity.getExtra1();
        if (extra1 != null) {
            stmt.bindString(10, extra1);
        }
 
        String extra2 = entity.getExtra2();
        if (extra2 != null) {
            stmt.bindString(11, extra2);
        }
 
        String extra3 = entity.getExtra3();
        if (extra3 != null) {
            stmt.bindString(12, extra3);
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public MemberEntity readEntity(Cursor cursor, int offset) {
        MemberEntity entity = new MemberEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // groupId
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // friendsId
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // userId
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // memberId
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // avatar
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // name
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // nick
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // remark
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // extra1
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // extra2
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11) // extra3
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, MemberEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setGroupId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setFriendsId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setUserId(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setMemberId(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setAvatar(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setName(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setNick(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setRemark(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setExtra1(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setExtra2(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setExtra3(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(MemberEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(MemberEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(MemberEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "list" to-many relationship of FriendsEntity. */
    public List<MemberEntity> _queryFriendsEntity_List(Long friendsId) {
        synchronized (this) {
            if (friendsEntity_ListQuery == null) {
                QueryBuilder<MemberEntity> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.FriendsId.eq(null));
                friendsEntity_ListQuery = queryBuilder.build();
            }
        }
        Query<MemberEntity> query = friendsEntity_ListQuery.forCurrentThread();
        query.setParameter(0, friendsId);
        return query.list();
    }

    /** Internal query to resolve the "list" to-many relationship of GroupEntity. */
    public List<MemberEntity> _queryGroupEntity_List(Long groupId) {
        synchronized (this) {
            if (groupEntity_ListQuery == null) {
                QueryBuilder<MemberEntity> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.GroupId.eq(null));
                groupEntity_ListQuery = queryBuilder.build();
            }
        }
        Query<MemberEntity> query = groupEntity_ListQuery.forCurrentThread();
        query.setParameter(0, groupId);
        return query.list();
    }

}
